package com.gestionabsences.filters;

import com.gestionabsences.beans.WebUser;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *
 * @author matt
 */
public class IsSecretary implements Filter {
    
    public static final String ATT_SESSION_WEBUSER = "sessionWebuser";
    
    public static final String URL_REDIRECTION = "connection";
    
    /**
     *
     * @param request The servlet request we are processing
     * @param response The servlet response we are creating
     * @param chain The filter chain we are processing
     *
     * @exception IOException if an input/output error occurs
     * @exception ServletException if a servlet error occurs
     */
    @Override
    public void doFilter(ServletRequest req, ServletResponse res,
	    FilterChain chain)
	    throws IOException, ServletException {
	/* Cast des objets request et response */
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;

        /* Récupération de la session depuis la requête */
        HttpSession session = request.getSession();

	WebUser user = (WebUser) session.getAttribute( ATT_SESSION_WEBUSER );
	
        if ( user != null && user.getType() != WebUser.TypeUser.STRING_THREE ) {
            /* Redirection vers la page de connection */
            request.getRequestDispatcher( URL_REDIRECTION ).forward( request, response );
	    //response.sendRedirect( URL_REDIRECTION );
        } else {
            /* Affichage de la page restreinte */
            chain.doFilter( request, response );
        }
    }

    /**
     * Destroy method for this filter
     */
    @Override
    public void destroy() {	
    }

    /**
     * Init method for this filter
     */
    @Override
    public void init(FilterConfig filterConfig) {
    }
}
